//
//  VIZOriginalView.swift
//  VIZ微博
//
//  Created by 位立志 on 16/4/13.
//  Copyright © 2016年 位立志. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

let margin = 8

//原始的
class VIZOriginalView: UIView {
    
    var viewBottom : Constraint?
    
    var cellViewModel:VIZCellViewModel? {
        
        didSet{
            //头像
            iconView.sd_setImageWithURL(cellViewModel?.headImageURL)
            mbRankImageView.image = cellViewModel?.mbImage
            
            //用户认证类型
            verifiedImageView.image = cellViewModel?.verifiedImage
            
            //微博正文
            contentLabel.text = cellViewModel?.homeStatuse?.text
            contentLabel.textAlignment = .Left
            //用户名称
            nameLabel.text = cellViewModel?.homeStatuse?.user?.name
//            //微博原始图片设置数据源
//            picture.imageURLs = cellViewModel?.homeStatuse?.pic_urls
            self.viewBottom?.uninstall()
            //微博图片设置数据源
            if let urls = cellViewModel?.homeStatuse?.pic_urls where urls.count > 0 {
                //有配图
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.viewBottom = make.bottom.equalTo(picture.snp_bottom).offset(8).constraint
                })
                picture.imageURLs = urls
                picture.hidden = false
            }else{
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.viewBottom = make.bottom.equalTo(contentLabel.snp_bottom).offset(8).constraint
                })
                picture.imageURLs = nil
                picture.hidden = true
                
            }
        }
    
    }
    
    
    

    /// 用户头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    /// 用户等级
    private lazy var mbRankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    /// 用户认证类型
    private lazy var verifiedImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    /// 用户名
    private lazy var nameLabel: UILabel = UILabel(text: "15期的童鞋们", textColor: UIColor.darkGrayColor(), fontSize: 15)
    /// 微博时间
    private lazy var timeLabel: UILabel = UILabel(text: "15:15", textColor: UIColor.orangeColor(), fontSize: 12)
    /// 微博来源
    private lazy var sourceLabel: UILabel = UILabel(text: "隔壁老王", textColor: UIColor.darkGrayColor(), fontSize: 12)
    /// 微博正文
    private lazy var contentLabel: UILabel = UILabel(text: "我没有男胖呀", textColor: UIColor.darkGrayColor(), fontSize: 15)
    
    /// 图片
    private lazy var picture : VIZPictureView = VIZPictureView()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setUI() {
        self.backgroundColor = UIColor.whiteColor()
        iconView.cornerRadius = 20
        //添加控件
        addSubview(iconView)
        addSubview(mbRankImageView)
        addSubview(verifiedImageView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(picture)
        //添加约束
        //头像图标
        iconView.snp_makeConstraints { (make) -> Void in
            make.left.top.equalTo(self).offset(margin)
            make.size.equalTo(CGSize(width: 40, height: 40))
        }
        //名称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconView.snp_right).offset(margin)
            make.top.equalTo(iconView.snp_top)
        }
        //等级图标
        mbRankImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_right).offset(margin)
            make.top.equalTo(nameLabel.snp_top)
        }
        //认证头像
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(iconView.snp_bottom)
            make.right.equalTo(iconView.snp_right)
        }
        //时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(iconView.snp_bottom)
            make.left.equalTo(iconView.snp_right).offset(margin)
        }
        //来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(iconView.snp_bottom)
            make.left.equalTo(timeLabel.snp_right).offset(margin)
        }
        //正文
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(iconView.snp_bottom).offset(margin)
            make.left.equalTo(iconView.snp_left)
            make.right.equalTo(self.snp_right).offset(-margin)
            //make.bottom.equalTo(self.snp_bottom).offset(-margin)
        }
        //图片
        picture.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(8)
            make.left.equalTo(contentLabel.snp_left)
            //make.size.equalTo(CGSize(width: 100, height: 100))
            
        }
        
        self.snp_makeConstraints { (make) -> Void in
            
            self.viewBottom = make.bottom.equalTo(picture.snp_bottom).offset(8).constraint
        }
        

        
    }
    
    
    
}
